/**
 * eg:dialog('用户信息','html','button');
 * footer eg : <a href="javascript:;" class="btn btn-primary">提交</a>
 * type eg : lager
 * 删除dialog : dialog.modal('hide');
 */
function dialog(title,content,footer,type){
	if(!content){
		content = '<div class="modal-loading">加载中..</div>';
	}
	var modal_id = 'modal-'+Math.round(100000+Math.random()*1000000);
	var html = '<div class="modal fade" id="'+modal_id+'">'+
				  '<div class="modal-dialog '+(type?' modal-'+type:'')+'">'+
				    '<div class="modal-content">'+
				      '<div class="modal-header">'+
				        '<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>'+
				        '<h4 class="modal-title">'+title+'</h4>'+
				      '</div>'+
				      '<div class="modal-body">'+content+'</div>'+
				      (footer?'<div class="modal-footer">'+footer+'</div>':'')+
				    '</div>'+
				  '</div>'+
				'</div>';
	$('body').append(html);
	$('#'+modal_id).modal('show');

	//隐藏后删除modal
	$('#'+modal_id).on('hidden.bs.modal',function(){
		$('#'+modal_id).remove();
	})

	return $('#'+modal_id);	
}


//confirm选择
//eg:confirm('是否要删除此用户？',['取消','确定'],callback);
function confirm(msg,btnArray,callback){
	if(typeof(btnArray) == 'function'){
		callback = btnArray;
		btnArray = ['取消','确定'];
	}

	var modal_id = 'modal-'+Math.round(100000+Math.random()*1000000);
	var html = '<div class="modal fade" id="'+modal_id+'">'+
				  '<div class="modal-dialog modal-sm">'+
				    '<div class="modal-content">'+				     	     
				      '<div class="modal-body" style="padding:30px 20px;"><span class="glyphicon glyphicon-question-sign" style="color:#f90; font-size:16px"></span> '+msg+'</div>'+
				      '<div class="modal-footer modal-footer-sm"><button type="button" class="btn btn-default btn-sm" data-dismiss="modal">'+btnArray[0]+'</button><button type="button" class="btn btn-primary btn-sm">'+btnArray[1]+'</button></div>'+
				    '</div>'+
				  '</div>'+
				'</div>';
	$('body').append(html);
	$('#'+modal_id).modal('show');

	//隐藏后删除modal
	$('#'+modal_id).on('hidden.bs.modal',function(){
		$('#'+modal_id).remove();
	})

	//绑定事件
	$('#'+modal_id+' .modal-footer button').on('click',function(){
		var index = $(this).index();
		var _this = $(this);
		var e = {
			index:index,
			modal:$('#'+modal_id),
			button:_this,
			remove:function(){
				$('#'+modal_id).modal('hide');
			}
		}
		callback(e);
	})
	
	return $('#'+modal_id);	
}


//提示框
var Toast = (function(){
    
    var preHtml = '';

	function loading(msg){
		if(!msg) msg = '加载中..';
		preHtml = '<div class="spinner"><div class="spinner-container container1"><div class="circle1"></div><div class="circle2"></div><div class="circle3"></div><div class="circle4"></div></div><div class="spinner-container container2"><div class="circle1"></div><div class="circle2"></div><div class="circle3"></div><div class="circle4"></div></div><div class="spinner-container container3"><div class="circle1"></div><div class="circle2"></div><div class="circle3"></div><div class="circle4"></div></div></div>';
		show(msg);
	}

	function info(msg,callback){
		preHtml = '<span class="glyphicon glyphicon-info-sign"></span>';
		show(msg);
		setTimeout(function(){
			destory();
			if(typeof(callback) == 'function') callback();
		},2000);
	}

	function warning(msg,callback){
		preHtml = '<span class="glyphicon glyphicon-exclamation-sign"></span>';
		show(msg);
		setTimeout(function(){
			destory();
			if(typeof(callback) == 'function') callback();
		},2000);
	}

	function error(msg,callback){
		preHtml = '<span class="glyphicon glyphicon-remove-sign"></span>';
		show(msg);
		setTimeout(function(){
			destory();
			if(typeof(callback) == 'function') callback();
		},2000);
	}

	function success(msg,callback){
		preHtml = '<span class="glyphicon glyphicon-ok-sign"></span>';
		show(msg);
		setTimeout(function(){
			destory();
			if(typeof(callback) == 'function') callback();
		},2000);
	}

	function show(msg){
		if($('#toast-div').length<1){
			var html = '<div id="toast-div">'+preHtml+'<div id="toast-div-content">'+msg+'</div></div>';
			$('body').append(html);
		}else{
			$('#toast-div').html(preHtml+'<div id="toast-div-content">'+msg+'</div>');
		}
	}

	function destory(){
		$('#toast-div').remove();
	}

	return {
		loading:loading,
		info:info,
		warning:warning,
		error:error,
		success:success,
		destory:destory
	};

})();


//图片上传
function uploadImg(upid,callback){
	var initParam = {
        'FilesAdded': function(up, files) {
            up.start();
        },
        
        'UploadProgress': function(up, file) {
        	$('#'+upid+'').html(file.percent + "%");
        },
        
        'FileUploaded': function(up, file, info) {
        	
        	if(info.response == 'error'){
        		Toast.error('Upload failed');
        	}else{
        		var result = $.parseJSON(info.response);
        		if(result.code != 0){
        			Toast.error('Upload failed');
        			return;
        		}
        		callback(result.data.img);
        	} 
        	$('#'+upid).html('UPLOAD');
        },
        'Error': function(up, err, errTip) {
            Toast.error(errTip);
        }
    };

    var uploader = new plupload.Uploader({
        browse_button : upid, //button id
        url : '/admin/upload/image', 
        flash_swf_url : '/assets/plupload/Moxie.swf', 
        chunk_size:'10mb', 
        max_retries:3, 
        auto_start: true,
        multi_selection:false,
        max_file_size : '100mb', 
        filters: [{title:'只能上传图片',extensions:'jpg,png,gif,jpeg'}],
        init:initParam
    });  

    uploader.init();
}

function datepicker(ele,lan,option){
	if(!lan) lan = 'chinese';
	if(!option){
		option = { format:"YYYY-MM-DD",isTime:false}
	}
	if(lan == 'english'){
		option.language = {
			name  : "en",
	        month : ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"],
	        weeks : [ "SUN","MON","TUR","WED","THU","FRI","SAT" ],
	        times : ["Hour","Minute","Second"],
	        clear : "Clear",
	        today : "Today",
	        yes   : "set",
	        close : "Close"
		}
	}
	$(ele).jeDate(option);
}

$("body").on('click', '.simple-action', function () {
	var that = this;
	var id = $(this).parents('td').attr('data-id');
	var url = $(this).attr('data-url');
	var action = $(this).attr('data-action');
	$.getJSON(url, function (json) {
			Toast.success('操作成功', function () {
				window.location.reload();
			})
	});
});